<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\store\model;

class StoreList extends \app\BaseModel
{
    protected $name = "massage_store_list";
    protected $append = ["admin_name"];
    public function getAdminIdAttr($value, $data)
    {
        if(!empty($value)) {
            $admin_model = new \app\massage\model\Admin();
            $admin = $admin_model->where(["id" => $value, "status" => 1, "agent_coach_auth" => 1])->count();
            return 0 < $admin ? $value : 0;
        }
        return 0;
    }
    public function getAdminNameAttr($value, $data)
    {
        if(!empty($data["admin_id"])) {
            $admin_model = new \app\massage\model\Admin();
            $name = $admin_model->where(["id" => $data["admin_id"]])->value("agent_name");
            return $name;
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function indexDataList($dis, $alh, $page = 10)
    {
        $data = $this->where($dis)->field(["title,cover,star,total_num,positive_rate,id,start_time,end_time,order_rate", $alh])->order("distance asc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function indexDataListV2($dis, $alh, $page = 10)
    {
        $data = $this->alias("a")->join("shequshop_school_admin b", "a.admin_id = b.id")->join("massage_store_cate_connect c", "a.id = c.store_id", "left")->where($dis)->field(["a.title,a.cover,a.star,a.total_num,a.positive_rate,a.id,a.start_time,a.end_time,a.order_rate", $alh])->group("a.id")->order("distance asc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function getStoreCoachId($store_id, $type = 1)
    {
        $coach_model = new \app\massage\model\Coach();
        $dis = ["b.status" => 1, "c.status" => 1, "c.agent_coach_auth" => 1];
        $where = [];
        if($type == 1) {
            $where[] = ["a.store_id", "=", $store_id];
        } else {
            $where[] = ["b.title", "like", "%" . $store_id . "%"];
        }
        $data = $coach_model->alias("a")->join("massage_store_list b", "a.store_id = b.id")->join("shequshop_school_admin c", "a.admin_id = c.id AND b.admin_id = c.id")->where($dis)->where($where)->column("a.id");
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function workStatus($store)
    {
        $start_time = strtotime($store["start_time"]);
        $end_time = strtotime($store["end_time"]);
        if($end_time <= $start_time) {
            if(time() < $end_time) {
                $start_time -= 86400;
            } else {
                $end_time += 86400;
            }
        }
        if($start_time <= time() && time() <= $end_time) {
            return 1;
        }
        return 0;
    }
    public function checkStoreStatus($store_id, $start_time, $end_time)
    {
        if(empty($store_id)) {
            return true;
        }
        $store = $this->dataInfo(["status" => 1, "id" => $store_id]);
        if(empty($store)) {
            return ["code" => 500, "msg" => "门店已经下架"];
        }
        $work_status = $this->checkStoreTime($store, $start_time, $end_time);
        if(!empty($work_status["code"])) {
            return $work_status;
        }
        return true;
    }
    public function checkStoreTime($store, $start_time, $end_time)
    {
        $all_day = 1;
        if($store["start_time"] != $store["end_time"]) {
            $all_day = 0;
        }
        if($all_day == 0) {
            $coach_start_time = strtotime($store["start_time"]) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $start_time));
            $coach_end_time = strtotime($store["end_time"]) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $start_time));
            if($end_time < $coach_start_time) {
                $coach_start_time -= 86400;
                $coach_end_time -= 86400;
            }
            $coach_end_time = $coach_start_time < $coach_end_time ? $coach_end_time : $coach_end_time + 86400;
            if($start_time < $coach_start_time || $coach_end_time < $end_time) {
                return ["code" => 500, "msg" => "不在门店营业时间内,门店营业时间:" . $store["start_time"] . "-" . $store["end_time"]];
            }
        }
        return true;
    }
}

?>